#define _CRT_SECURE_NO_WARNINGS 1
#include "Tree.h"


void PreOrder(BTNode* root)
{
	if (root==NULL)
	{
		return;
	}
	printf("%d ", root->data);
	PreOrder(root->left);
	PreOrder(root->right);
}

void InOrder(BTNode* root)
{
	if (root==NULL)
	{
		return;
	}
	InOrder(root->left);
	printf("%d ", root->data);
	InOrder(root->right);
}

void PostOrder(BTNode* root)
{
	if (root==NULL)
	{
		return;
	}
	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}


int BinaryTreeSize(BTNode* root)
{
	if (root==NULL)
	{
		return 0;
	}
	return 1 + (BinaryTreeSize(root->left) + BinaryTreeSize(root->right));
}

int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}
	return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}

int BinaryTreeLevelKSize(BTNode* root, int k)
{
	if (root==NULL)
	{
		return 0;
	}
	if (k==1)
	{
		return 1;
	}
	return BinaryTreeLevelKSize(root->left, k - 1) + BinaryTreeLevelKSize(root->right, k - 1);
}


int BinaryTreeDepth(BTNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int leftdef = BinaryTreeDepth(root->left);
	int rightdef = BinaryTreeDepth(root->right);
	if (leftdef>rightdef)
	{
		return leftdef+1;
	}
	else
	{
		return rightdef + 1;
	}
}



BTNode* BinaryTreeFind(BTNode* root, BTDataType x)
{
	if (root==NULL)
	{
		return NULL;
	}

	if ((root->data) == x)
	{
		return root;
	}

	BTNode* leftfind = BinaryTreeFind(root->left, x);
	
	if(leftfind)
	{
		return leftfind;
	}
	BTNode*rightfind =  BinaryTreeFind(root->right, x);
	if(rightfind)
	{
		return rightfind;
	}

	return NULL;
}

void BinaryTreeDestory(BTNode** root)
{
	if (*root==NULL)
	{
		return;
	}
	BinaryTreeDestory(&(*root)->left);
	BinaryTreeDestory(&(*root)->right);
	free(*root);
	*root = NULL;
}